package com.sctvcloud.dongqu.ui.adapter.holder;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.gridsum.tracker.GridsumWebDissector;
import com.iflytek.cloud.thirdparty.V;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.beans.AnchorRecommendItem;
import com.sctvcloud.dongqu.beans.FProgramme;
import com.sctvcloud.dongqu.beans.NewsItem;
import com.sctvcloud.dongqu.ui.activities.ReviewActivity;
import com.sctvcloud.dongqu.ui.adapter.AnchorHomePageAdapter;
import com.sctvcloud.dongqu.ui.utils.GlideUtil;
import com.sctvcloud.dongqu.ui.utils.SkipUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/13 0013.
 */

public class AnchorRecommendHolder extends BaseAnchorHolder<AnchorRecommendItem> implements View.OnClickListener {
    @BindView(R.id.item_anchor_recommend_img)
    protected CustomEXImageView img;
    @BindView(R.id.item_anchor_recommend_lable)
    protected CustomFontTextView lable;
    @BindView(R.id.item_anchor_recommend_title)
    protected CustomFontTextView title;
    @BindView(R.id.item_anchor_recommend_playTime)
    protected CustomFontTextView playTime;
    @BindView(R.id.item_anchor_recommend_intro)
    protected CustomFontTextView intro;
    @BindView(R.id.item_anchor_recommend_update)
    protected CustomFontTextView update;

    @BindView(R.id.item_anchor_recommend_c1)
    protected CardView card1;
    @BindView(R.id.item_anchor_recommend_c2)
    protected CardView card2;

    @BindView(R.id.item_anchor_recommend_img2)
    protected CustomEXImageView img2;
    @BindView(R.id.item_anchor_recommend_lable2)
    protected CustomFontTextView lable2;
    @BindView(R.id.item_anchor_recommend_title2)
    protected CustomFontTextView title2;
    @BindView(R.id.item_anchor_recommend_playTime2)
    protected CustomFontTextView playTime2;
    @BindView(R.id.item_anchor_recommend_intro2)
    protected CustomFontTextView intro2;
    @BindView(R.id.item_anchor_recommend_update2)
    protected CustomFontTextView update2;

    @BindView(R.id.layout_title)
    protected View headView;
    @BindView(R.id.iv_item_anchor_title)
    protected ImageView ivHeadView;
    @BindView(R.id.tv_item_anchor_title_name)
    protected CustomFontTextView tvHeadText;
    @BindView(R.id.v_bottom_line)
    protected View vBottomLine;

    protected String programUrl;
    private NewsItem newsItem;
    private String showStr;

    protected String programUrl2;
    private NewsItem newsItem2;
    private String showStr2;

    public AnchorRecommendHolder(Context context, View itemView) {
        super(context, itemView);
        card1.setOnClickListener(this);
        card2.setOnClickListener(this);
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(AnchorRecommendItem fProgramme) {
        setCard1Data(fProgramme.getProgramme1());
        if (fProgramme.getProgramme2() != null) {
            card2.setVisibility(View.VISIBLE);
            setCard2Data(fProgramme.getProgramme2());
        } else {
            card2.setVisibility(View.GONE);
        }
        if (fProgramme.isStick()) {
            itemView.setTag(AnchorHomePageAdapter.HAS_STICKY_VIEW);
            headView.setVisibility(View.VISIBLE);
            ivHeadView.setVisibility(View.GONE);
            tvHeadText.setText("主播推荐");
        } else {
            itemView.setTag(AnchorHomePageAdapter.NONE_STICKY_VIEW);
            headView.setVisibility(View.GONE);
        }
        itemView.setContentDescription("主播推荐");
        if (fProgramme.isLast()) {
            vBottomLine.setVisibility(View.VISIBLE);
        } else {
            vBottomLine.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        String url = programUrl;
        if (v.getId() == R.id.item_anchor_recommend_c2) {
            url = programUrl2;
        }
        Intent intent = new Intent(context, ReviewActivity.class);
        if (!TextUtils.isEmpty(url) && url.endsWith(".json")) {
            intent.putExtra(BaseActivity.EX_ID, url);
            context.startActivity(intent);
        } else {
            Toast.makeText(context, "url错误", Toast.LENGTH_SHORT).show();
        }

    }

    @OnClick({R.id.item_anchor_recommend_update, R.id.item_anchor_recommend_update2})
    public void latestOnClick(View view) {
//        Intent intent = new Intent(context, NewesDetailActivity.class);
//        intent.putExtra(NewesDetailActivity.EX_URL, CollectionUtils.convertData(newsItem));
//        context.startActivity(intent);
        if (view == null) {
            return;
        }
        int id = view.getId();
        pairs.add(new Pair<String, String>("位置", "主播推荐"));
        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> p : pairs) {
            map.put(p.first, p.second);
        }
        String title = "";
        if (id == R.id.item_anchor_recommend_update) {
            title = newsItem.getNewsTitle();
            SkipUtil.skipWithNewsItem(context, newsItem, map);

        } else if (id == R.id.item_anchor_recommend_update2) {
            title = newsItem2.getNewsTitle();
            SkipUtil.skipWithNewsItem(context, newsItem2, map);
        }
        if (fragment != null) {
            GridsumWebDissector.getInstance().trackEvent(fragment, "", title, "", 200, pairs);

        } else if (activity != null) {
            GridsumWebDissector.getInstance().trackEvent(activity, "", title, "", 200, pairs);

        }
    }

    private void setCard1Data(FProgramme fProgramme) {
        if (!TextUtils.isEmpty(fProgramme.getProgramType())) {
            lable.setText(fProgramme.getProgramType());
//            lable.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(fProgramme.getProgramTitle())) {
            title.setText(fProgramme.getProgramTitle());
        }
        String total = fProgramme.getProgramTotal();
        String amout = fProgramme.getPlayAmount();
        if (!TextUtils.isEmpty(total) && !TextUtils.equals("0", total)) {
            showStr = "总" + total + "期";
        }
        if (!TextUtils.isEmpty(amout) && !TextUtils.equals("0", amout)) {
            showStr = "播放" + amout + "次";
        }
        if (!TextUtils.isEmpty(total) && !TextUtils.equals("0", total) && !TextUtils.isEmpty(amout) && !TextUtils.equals("0", amout)) {
            showStr = "总" + total + "期" + "|" + "播放" + amout + "次";
        }
        playTime.setText(showStr);
        if (!TextUtils.isEmpty(fProgramme.getProgramIntro())) {
            intro.setText(fProgramme.getProgramIntro());
        } else {
            intro.setText("简介:");
        }
        if (!TextUtils.isEmpty(fProgramme.getUpdateInfo())) {
            update.setText("更新至:" + fProgramme.getUpdateInfo() + "期");
        } else {
            update.setText("");
        }
        if (!TextUtils.isEmpty(fProgramme.getProgramImage())) {
            GlideUtil.getGlideWithSmall169Def(context, fProgramme.getProgramImage()).into(img);
        } else {
            img.setImageResource(R.mipmap.icon_def_16_9_s);
        }
        programUrl = fProgramme.getContentUrl();
        newsItem = fProgramme.getProgramme();
    }

    private void setCard2Data(FProgramme fProgramme) {
        if (!TextUtils.isEmpty(fProgramme.getProgramType())) {
            lable2.setText(fProgramme.getProgramType());
//            lable.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(fProgramme.getProgramTitle())) {
            title2.setText(fProgramme.getProgramTitle());
        }
        String total = fProgramme.getProgramTotal();
        String amout = fProgramme.getPlayAmount();
        if (!TextUtils.isEmpty(total) && !TextUtils.equals("0", total)) {
            showStr2 = "总" + total + "期";
        }
        if (!TextUtils.isEmpty(amout) && !TextUtils.equals("0", amout)) {
            showStr2 = "播放" + amout + "次";
        }
        if (!TextUtils.isEmpty(total) && !TextUtils.equals("0", total) && !TextUtils.isEmpty(amout) && !TextUtils.equals("0", amout)) {
            showStr2 = "总" + total + "期" + "|" + "播放" + amout + "次";
        }
        playTime2.setText(showStr2);
        if (!TextUtils.isEmpty(fProgramme.getProgramIntro())) {
            intro2.setText(fProgramme.getProgramIntro());
        } else {
            intro2.setText("简介:");
        }
        if (!TextUtils.isEmpty(fProgramme.getUpdateInfo())) {
            update2.setText("更新至:" + fProgramme.getUpdateInfo() + "期");
        } else {
            update2.setText("");
        }
        if (!TextUtils.isEmpty(fProgramme.getProgramImage())) {
            GlideUtil.getGlideWithSmall169Def(context, fProgramme.getProgramImage()).into(img2);
        } else {
            img2.setImageResource(R.mipmap.icon_def_16_9_s);
        }
        programUrl2 = fProgramme.getContentUrl();
        newsItem2 = fProgramme.getProgramme();
    }
}
